#coding=UTF-8 
# from selenium.webdriver.common.action_chains import ActionChains



from chinacQA.TestCase import TestCase
from chinacQA.TestLink_TestCase import assertBool
from chinacQA.TestLink_TestCase import getConfigure
from chinacQA.TestLink_TestCase import waitMoment
from template.NeoCUUI import *


def STEP_CreateFirewall():
    SecurityMenu_01.click()
    waitMoment(3)
    FireWallMenu.click()
    heads = securitygroupsTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    waitMoment(3)
    securitygroups_action_create.click()
    Firewallidname.text = getConfigure("FirewallName_01")
    Firewalliddescription.text = getConfigure("FirewallDec_01")
    FirewallCreateButton.click()
    waitMoment(3)
    status = False
    for i in range(len(securitygroupsTable.getRows())):
        if(securitygroupsTable.getCell(i,columnName).text.strip()==getConfigure("FirewallName_01")): 
            status = True
            break
    assertBool(status,"3465")

def STEP_EditFirewall():
    FireWallMenu.click()
    heads = securitygroupsTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    waitMoment(3)
    if("没有要显示的条目" in securitygroupsTable.getCell(0,0).text):
        assertBool(False,"3467")
        return
    for i in range(len(securitygroupsTable.getRows())):
        if(securitygroupsTable.getCell(i,columnName).text==getConfigure("FirewallName_01")):       
            actioncell=securitygroupsTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="编辑防火墙"):
                    button.click()
                    break
            break
    FirewallEditidname.text = getConfigure("FirewallNewName")
    FirewallEditdesc.text = getConfigure("FirewallNewDec")
    FirewallEditCommit.click()
    
    waitMoment(3)
    status = False
    for i in range(len(securitygroupsTable.getRows())):
        if(securitygroupsTable.getCell(i,columnName).text.strip()==getConfigure("FirewallNewName")): 
            status = True
            break
    assertBool(status,"3467")
    
def STEP_DeleteFirewall():
    FireWallMenu.click()
    heads = securitygroupsTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    waitMoment(3)
    if("没有要显示的条目" in securitygroupsTable.getCell(0,0).text):
        assertBool(False,"3469")
        return
    for i in range(len(securitygroupsTable.getRows())):
        if(securitygroupsTable.getCell(i,columnName).text==getConfigure("FirewallNewName")):       
            actioncell=securitygroupsTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="删除防火墙"):
                    button.click()
                    break
            break
    FirewallDeleteCommit.click()
    
    waitMoment(3)
    if("没有要显示的条目" in securitygroupsTable.getCell(0,0).text):
        assertBool(True,"3469")
        return
    status = True
    for i in range(len(securitygroupsTable.getRows())):
        if(securitygroupsTable.getCell(i,columnName).text.strip()==getConfigure("FirewallNewName")): 
            status = False
            break
    assertBool(status,"3469")


def STEP_CreateKeyPair():
    KeyPairMenu.click()
    waitMoment(3)
    keypair_action_addkeypair.click()
    Keypairidname.text = getConfigure("KeyPairName_01")
    KeypairCreate.click()
#     KeyPairPage.back()
#     print "d"
#     print KeyPairPage.title
#     KeyPairPage.back()
    waitMoment(3)
    KeyPairMenu1.click()
#     KeyPairMenu.click()
    waitMoment(3)
    heads = keypairTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    waitMoment(3)
    if("没有要显示的条目" in keypairTable.getCell(0,0).text):
        assertBool(False,"3471")
        return
    status = False
    for i in range(len(keypairTable.getRows())):
        if(keypairTable.getCell(i,columnName).text==getConfigure("KeyPairName_01")):
            status = True
            break    
    assertBool(status,"3471")
    
    
def STEP_ImportKeyPair():
    pass

def STEP_DeleteKeyPair():
    KeyPairMenu.click()
    waitMoment(3)
    heads = keypairTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    if("没有要显示的条目" in keypairTable.getCell(0,0).text):
        assertBool(False,"3475")
        return
    
    for i in range(len(keypairTable.getRows())):
        if(keypairTable.getCell(i,columnName).text==getConfigure("KeyPairName_01")):
            actioncell=keypairTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            KeyPairDelete.click()
            break    
    if("没有要显示的条目" in keypairTable.getCell(0,0).text):
        assertBool(True,"3475")
        return
    status = True
    for i in range(len(keypairTable.getRows())):
        if(keypairTable.getCell(i,columnName).text==getConfigure("KeyPairName_01")):
            status = False
            break
    assertBool(status,"3475")
    
    
def Login(user, pw):
    idusername.text=user
    idpassword.text=pw
    loginBtn.click()

def STEP_UserLogin():
    Login(getConfigure("User"),getConfigure("UserNewPassword"))
    
def STEP_Clean():
    adminrole.click()
    waitMoment(6)
    loginout.click()


TC_005_NeoCUSecurity  = TestCase("Neocu Security")
TC_005_NeoCUSecurity.AddStep(STEP_UserLogin)
TC_005_NeoCUSecurity.AddStep(STEP_CreateFirewall)
TC_005_NeoCUSecurity.AddStep(STEP_EditFirewall)
TC_005_NeoCUSecurity.AddStep(STEP_DeleteFirewall)
TC_005_NeoCUSecurity.AddStep(STEP_Clean)


if __name__ == "__main__":
    TC_005_NeoCUSecurity.run()

    


    
